// ==========================================================================
// gin-sns自动生成业务逻辑层相关代码，只生成一次，按需修改,再次生成不会覆盖.
// date：2021-02-21 13:45:54 +0800 CST
// path: app/service/module/topic_service.go
// author：jon
// ==========================================================================
package module

import (
	"errors"
	"gin-sns/app/dao/module/topic"
	topicModel "gin-sns/app/model/module/topic"
	"time"

	"github.com/gin-gonic/gin"

	"gin-sns/app/ginframe/utils/convert"
	"gin-sns/app/ginframe/utils/page"
)

var TopicService = newTopicService()

func newTopicService() *topicService {
	return &topicService{}
}

type topicService struct {
}

//根据主键查询数据
func (s *topicService) SelectRecordById(id int64) (*topicModel.Entity, error) {
	return topic.TopicDao.SelectById(id)
}

//
func (s *topicService) SelectRecordsIn(idarr []int64) ([]topicModel.Entity, error) {

	list, err := topic.TopicDao.FindIn("id", idarr)
	if list != nil && len(list) > 0 {
		err = errors.New("not found")
		list = nil
	}
	return list, err
}

//根据主键删除数据
func (s *topicService) DeleteRecordById(id int64) bool {
	return topic.TopicDao.DeleteById(id)
}

//批量删除数据记录
func (s *topicService) DeleteRecordByIds(ids string) int64 {
	ida := convert.ToInt64Array(ids, ",")
	result, err := topic.TopicDao.DeleteBatch(ida...)
	if err != nil {
		return 0
	}
	return result
}

//添加数据
func (s *topicService) AddSave(req *topicModel.AddReq, ctx *gin.Context) (int64, error) {
	var entity topicModel.Entity

	//entity.Id = req.Id
	entity.ParentId = req.ParentId
	entity.Title = req.Title
	entity.Description = req.Description
	entity.Logo = req.Logo
	entity.CountPost = req.CountPost
	entity.CountView = req.CountView
	entity.CountFollow = req.CountFollow
	entity.Status = req.Status
	entity.CreateTime = time.Now()
	//entity.CreateBy = ""

	//user := system.UserService.GetProfile(ctx)

	//if user != nil {
	//	entity.CreateBy = user.LoginName
	//}

	_, err := topic.TopicDao.Insert(&entity)
	return entity.Id, err
}

//修改数据
func (s *topicService) EditSave(req *topicModel.EditReq, ctx *gin.Context) (int64, error) {
	entity := &topicModel.Entity{Id: req.Id}
	ok, err := topic.TopicDao.FindOne(entity)

	if err != nil {
		return 0, err
	}

	if !ok {
		return 0, errors.New("数据不存在")
	}

	entity.ParentId = req.ParentId
	entity.Title = req.Title
	entity.Description = req.Description
	entity.Logo = req.Logo
	entity.CountPost = req.CountPost
	entity.CountView = req.CountView
	entity.CountFollow = req.CountFollow
	entity.Status = req.Status
	//entity.UpdateTime = time.Now()
	//entity.UpdateBy = ""

	//user := system.UserService.GetProfile(ctx)

	//if user == nil {
	//	entity.UpdateBy = user.LoginName
	//}

	return topic.TopicDao.Update(entity)
}

//根据条件查询数据
func (s *topicService) SelectListAll(params *topicModel.SelectPageReq) ([]topicModel.Entity, error) {
	return topic.TopicDao.SelectListAll(params)
}

//根据条件分页查询数据
func (s *topicService) SelectListByPage(params *topicModel.SelectPageReq) ([]topicModel.Entity, *page.Paging, error) {
	return topic.TopicDao.SelectListByPage(params)
}

// 导出excel
func (s *topicService) Export(param *topicModel.SelectPageReq) (string, error) {
	head := []string{"话题id", "父级id", "话题名称", "话题简介", "话题图片logo", "文章数量", "阅读数量", "被关注数量", "0未审核 1正常状态 2审核未通过 3删除", "创建时间"}
	col := []string{"id", "parent_id", "title", "description", "logo", "count_post", "count_view", "count_follow", "status", "create_time"}
	return topic.TopicDao.SelectListExport(param, head, col)
}

// 浏览数+1
func (s *topicService) IncrFollowCount(followId int64) {
	topic.TopicDao.IncrFollowCount(followId)
}

func (s *topicService) DecrFollowCount(followId int64) {
	topic.TopicDao.DecrFollowCount(followId)
}

// 浏览数+1
func (s *topicService) IncrViewCount(followedId int64) {
	topic.TopicDao.IncrViewCount(followedId)
}

func (s *topicService) DecrViewCount(followedId int64) {
	topic.TopicDao.DecrViewCount(followedId)
}

// 浏览数+1
func (s *topicService) IncrPostCount(followedId int64) {
	topic.TopicDao.IncrPostCount(followedId)
}

func (s *topicService) DecrPostCount(followedId int64) {
	topic.TopicDao.DecrPostCount(followedId)
}
